<?php
namespace Home\Controller;
use Think\Controller;
class CashCropController extends Controller{
    public function index(){
        $this->display();
    }
    //添加农户
    public function addFamert(){
        $this -> display();
    }
    //保存农户
    public function saveFamert(){
        $data = array();
        $j = array();
        $data = I('post.');
        $data['uc_time'] = time();
        $rs = M('User_crops') -> data($data) -> add();
        if($rs){
            $j['status'] = 100;
            $j['info'] = '添加农户成功';
            $j['uc_id'] = $rs;
        }else{
            $j['status'] = 101;
            $j['info'] = '添加农户失败，请联系管理员';
        }
        $this->ajaxReturn($j);
    }
    //添加农户信息
    public function addData(){
        $uc_id = I('get.uc_id')?I('get.uc_id'):0;
        if($uc_id){
            $rs = M('User_crops')->field('uc_name,uc_id,uc_card')->where('uc_id='.$uc_id)->find();
            $this->assign('user',$rs);
        }
        $this->display();   
    }
    //保存农户信息
    public function saveData(){
        $uc_id = I('uc_id');
        $j = array();
        $data = array();
        if(!$uc_id){
            $j['status'] = 103;
            $j['info'] = '没有此用户，请添加农户信息';
            $this -> ajaxReturn($j);
        }    
        $data = I('post.');
        //var_dump($data);
        $data['uc_id'] = $uc_id;
        //拼接种植方式
        if(is_array($data['c_plant_type'])){

            $type = '';
            foreach ($data['c_plant_type'] as $k => $v) {
                //dump($v);
                switch ($v) {
                    case '庭院':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_patio'];
                        break;
                    case '露地':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_land'];
                        break;
                    case '设施-温室':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_house'];
                        break;
                    case '设施-冷棚':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_coldshed'];
                        break;
                }
            }
            $data['c_plant_type'] = $type;
        }
        //拼接存储方式
        if(is_array($data['c_storage_type'])){
            $data['c_storage_type']=$this ->stitching($data['c_storage_type']);
        }
        //置换
        // var_dump($data);
        // return false;
        //拼接销售方式
        if(is_array($data['c_sale_way'])){
            $way_type = '';
            foreach ($data['c_sale_way'] as $k => $v) {
                switch ($v) {
                    case '直销':
                        if($way_type){$way_type .= ',';}
                        $way_type .= $v.':'.$data['c_sale_way_zx_name'];
                        break;
                    case '中介':
                        if($way_type){$way_type .= ',';}
                        $way_type .= $v.':'.$data['c_sale_way_zj_name'];
                        break;
                }
            }
            $data['c_sale_way'] = $way_type;
        }
        //拼接销售市场
        if(is_array($data['c_sale_market'])){
            $mar_type = '';
            foreach ($data['c_sale_market'] as $k => $v) {
                switch ($v) {
                    case '本市':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_bshi'];
                        break;
                    case '本省':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_bs'];
                        break;
                    case '国内':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_gn'];
                        break;
                    case '境外':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_jw'];
                        break;
                }
            }
            $data['c_sale_market'] = $mar_type;
        }
        //拼接农产品认证标志
        if(is_array($data['c_authentication'])){
            $authen = '';
            foreach ($data['c_authentication'] as $k => $v) {
                switch ($v) {
                    case '无公害农产品':
                        if($authen){$authen .= ',';}
                        $authen .= ('无公害农产品:'.'1');
                        break;
                    case '绿色食品':
                        if($authen){$authen .= ',';}
                        $authen .= ('绿色食品:'.$data['c_authentication_green']);
                        break;
                    case '有机食品':
                        if($authen){$authen .= ',';}
                        $authen .= ('有机食品:'.$data['c_authentication_yj']);
                        break;
                }
            }
            $data['c_authentication'] = $authen;
        }
        //拼接应季时间，反季时间
        if($data['yj_time']){$data['c_sale_time_yj'] = $data['yj_time'];}
        if($data['fj_time']){$data['c_sale_time_fj'] = $data['fj_time'];}
        //拼接应季价格，反季价格
        if($data['yj_money']){$data['c_sale_money_yj'] = $data['yj_money'];}
        if($data['fj_money']){$data['c_sale_money_fj'] = $data['fj_money'];}

        
        $data['c_time'] = time();
        //return false;
        $rs = M('Crops')->data($data)->add();
        if($rs){
            $j['status'] = 100;
            $j['info'] = '添加农户信息成功';
        }else{
            $j['status'] = 101;
            $j['info'] = '添加农户信息失败，请联系管理员';
        }
        $this->ajaxReturn($j);
    }
    //获取村
    public function getVillage($pid){
    	$village = M('Areas') -> where('pid = '.$pid) -> field('area_name,id') ->select();
    	//print_r($village);
    	$this -> ajaxReturn($village);
    }
    //获取镇
    public function getTwon(){
        $village = M('Areas') -> where('pid = 1') -> field('area_name,id') ->select();
        //print_r($village);
        $this -> ajaxReturn($village);
    }
    // 用身份证获取用户id
    public function getUser($uc_card,$type){
        $data = array();
        if($type == 1){
            $data['uc_name'] = $uc_card;
        }else{
            $data['uc_card'] = $uc_card;
        }
        $rs = M('User_crops')->where($data)->field('uc_id,uc_name,uc_card')->find();
        $j = array();
        if($rs){
            $j['status'] = 100;
            $j['info'] = '';
            $j['uc_id'] = $rs['uc_id'];
            $j['uc_name'] = $rs['uc_name'];
            $j['uc_card'] = $rs['uc_card'];
        }else{
            $j['status'] = 101;
            $j['info'] = '该用户没有录入，请先录入用户';
        }
        $this -> ajaxReturn($j);
    }
    //拼接字符串方法
    private function stitching($arr){
        $type = '';
        if(!is_array($arr)){return false;}
        foreach ($arr as $v) {
            if(!$type){$type = $v;}else{$type=$type.",".$v;}                       
        }
        $arr=$type;
        return $arr;
    }
    //拼接销售市场
    private function stit($arr){
        $type = '';
        if(!is_array($arr)){return false;}
        foreach ($arr as $v) {
            if(!$type){$type = $v;}else{$type=$type.":".$v.',';}                       
        }
        $arr=$type;
        return $arr;
    }
    //获取粮食分类
    public function getCrops(){
        $crops = M('Crops_type') -> where('ct_f_id = 0') -> field('ct_name,ct_id') ->select();
        //print_r($village);
        $this -> ajaxReturn($crops); 
    }
    //获取粮食名称
    public function getCropsname($pid){
        $crops = M('Crops_type') -> where('ct_f_id = '.$pid) -> field('ct_name,ct_id') ->select();
        //print_r($village);
        $this -> ajaxReturn($crops); 
    }
    //获取身份证是否存在、用户表
    public function getCard(){  
        $data = array();
        $uc_card = I('post.uc_card');
        $data['uc_card'] = $uc_card;
        $rs = M('User_crops')->where($data)->find();
        $j = array();
        if($rs){
            $j['status'] = 101;
            $j['info'] = '该用户已存在用户表中，请直接录入农作物信息';
        }else{
            $j['status'] = 100;
            $j['info'] = '';
        }
        $this -> ajaxReturn($j);  
    }
}

